<template>
    <el-dialog class="car-owner-add" :title="isEdit?'信息修改':'新增参数'" :visible.sync="dialogVisible" width="800px" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="参数名" prop="configName">
                        <el-input v-model="ruleForm.configName"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="参数值" prop="configValue">
                        <el-input v-model="ruleForm.configValue"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="参数key" prop="configKey">
                        <el-input v-model="ruleForm.configKey"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="参数类型" prop="configType">
                        <el-radio-group v-model="ruleForm.configType">
                            <el-radio label="Y">系统参数</el-radio>
                            <el-radio label="N">用户参数</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="ruleForm.remark"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button @click="doReset">重 置</el-button>
            <el-button type="primary" @click="doConfirm">确 定</el-button>
        </span>
    </el-dialog>
</template>


<script>
import * as API from '@/api/admin/config'
export default {
    data() {
        return {
            dialogVisible: false,
            isEdit: false,
            fileList: [],
            filePath: null,
            ruleForm: {
                configName: '',
                configType: '',
                configValue: '',
                configId: '',
                remark: '',
                configKey: ''
            },
            rules: {
                configName: [{ required: true, message: '输入参数名', trigger: 'blur' }],
                configType: [{ required: true, message: '参数类型', trigger: 'blur' }],
                configValue: [{ required: true, message: '输入参数值', trigger: 'blur' }],
                configId: [{ required: false, message: '请选择所在小区', trigger: 'blur' }],
                configKey: [{ required: true, message: '输入参数key值', trigger: 'blur' }],
                remark: [{ required: false, message: '参数说明', trigger: 'blur' }]
            }
        }
    },
    methods: {
        show() {
            this.dialogVisible = true
        },
        edit(ruleForm) {
            Object.assign(this.ruleForm, ruleForm)
            this.isEdit = true
            this.dialogVisible = true
        },
        handleClose() {
            this.doReset()
            this.dialogVisible = false
        },
        doConfirm() {
            this.$refs['ruleForm'].validate(valid => {
                if (valid) {
                    if (this.isEdit) {
                        API.save(this.ruleForm).then(res => {
                            if (res.data.code === 0) {
                                this.$message.success('参数修改成功!')
                                this.handleClose()
                                this.$emit('table-search')
                            }
                        })
                    } else {
                        API.save(this.ruleForm).then(res => {
                            if (res.data.code === 0) {
                                this.$message.success('参数新增成功!')
                                this.handleClose()
                                this.$emit('table-search')
                            }
                        })
                    }
                }
            })
        },
        doReset() {
            this.$refs['ruleForm'].resetFields()
        }
    }
}
</script>

<style lang="scss" scoped>
.car-owner-add {
    .el-input {
        width: 220px;
    }
}
</style>
